package org.wuqi.microweb.api.service.impl;

import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.wuqi.microweb.api.config.JdWxSmsConfig;
import org.wuqi.microweb.api.service.SmsCodeRegister;

import javax.swing.text.html.parser.Entity;
import java.io.IOException;
import java.io.InputStream;
import java.net.http.HttpClient;
import java.util.Random;

/**
 * @author jjj
 * @program: earnest-money
 * @description:TODO
 * @date 2024-01-05 16:22:21
 */
public class SmsCodeRegisterImpl implements SmsCodeRegister {
    @Resource
    private JdWxSmsConfig jdWxSmsConfig;
    private RedisTemplate redisTemplate;

    @Override
    public Boolean sendSms(String phoneNumber) {
//        生成四位验证码
        String code = RandomStringUtils.randomNumeric(4);
        String content = """
                你的验证码是：%s，3分钟有效，请勿泄露给其他人
                """;
//        将四位验证码和获取的手机号存入redis；
//        redisTemplate.opsForValue().set(phoneNumber,code);
        String content2 = String.format(content, code);
//        使用HttpClient发送请求，
        //1.获得一个httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
//2.生成一个get请求
        HttpGet httpget = new HttpGet("http://localhost/");
//3.执行get请求并返回结果
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpget);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
//                成功响应
                String string = EntityUtils.toString(response.getEntity());
                JSONObject parse = JSONObject.parse(string);
                if("1000".equals(parse.getString("code"))){
                    
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            //相当于关闭浏览器
            try {
                httpclient.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return null;
    }
}
 
 